﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectProjectItemSeqActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var ids = this.AsyncDialog("Id", request.Model, "Select").Split(',');

            if (ids.Length > 0)
            {

                var sub = DataFactory.Instance().ProjectItem(UMC.Data.Utility.UInt64Parse(ids[0]).Value);
                var user = this.Context.Token.Identity();

                if (sub.project_id.HasValue)
                {
                    var member = DataFactory.Instance().ProjectMember(sub.project_id.Value, user.Id.Value);
                    if (member != null)
                    {
                        switch (member.AuthType)
                        {
                            case WebAuthType.Admin:
                            case WebAuthType.User:
                                break;
                            default:
                                this.Prompt("您未有编辑此图文的权限");
                                break;
                        }

                    }
                    else
                    {
                        this.Prompt("您未有编辑此图文的权限");
                    }
                }
            }
            if (ids.Length > 1)
            {
                for (var i = 0; i < ids.Length; i++)
                {
                    DataFactory.Instance().Put(new ProjectItem { Id = UMC.Data.Utility.UInt64Parse(ids[i]).Value, Sequence = i });
                }
            }
            else if (ids.Length == 1)
            {
                var projectItemId = UMC.Data.Utility.UInt64Parse(ids[0]);
                var portfolioId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Portfolio", "none"));
                if (portfolioId.HasValue && projectItemId.HasValue)
                {
                    var portfolio = DataFactory.Instance().Portfolio(portfolioId.Value);
                    var projectItem = DataFactory.Instance().ProjectItem(projectItemId.Value);
                    if (portfolio != null && projectItem != null)
                    {
                        if (portfolio.project_item_id.Value != projectItem.Id.Value)
                        {
                            this.AsyncDialog("Confirm", r => new UIConfirmDialog($"确认把{portfolio.Caption}文集转移到{projectItem.Caption}栏目吗") { Title = "提示" });

                            var subs = DataFactory.Instance().Subject(portfolio);
                            foreach (var sub in subs)
                            {
                                DataFactory.Instance().Put(new Subject { Id = sub.Id, project_item_id = projectItem.Id });
                            }
                            DataFactory.Instance().Put(new Portfolio { Id = portfolio.Id, project_item_id = projectItem.Id.Value });

                            var project = DataFactory.Instance().Project(projectItem.project_id.Value);
                            var subs2 = SubjectPortfolioSubActivity.Portfolio(project, DataFactory.Instance().ProjectItem(portfolio.project_item_id.Value), 1);
                            this.Context.Send("Portfolio.List", new WebMeta().Put("subs", subs2), true);

                        }
                    }
                }

            }


            response.Redirect(new WebMeta().Put("msg", ids.Length));


        }

    }
}